﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using CommonExtensions.ObjectExtensions;
using CommonExtensions.StringExtensions;
using DBMigrator.Core.DBProviders;
using StructureMap;
using StructureMap.Pipeline;

namespace DBMigrator.Core.Execution
{
    public class Migrator
    {
        private readonly Assembly assemblyContianingMigrations;
        private readonly string environment;

        public Migrator(Assembly assemblyContianingMigrations, string environment)
        {
            if(assemblyContianingMigrations.IsNull())
                throw new ArgumentNullException("assemblyContianingMigrations");
            if(environment.IsBlank())
                throw new ArgumentNullException("environment");

            StructureMapSetup.RunSetup(assemblyContianingMigrations, environment);
        }

        public void MigrateToLatest()
        {
            MigrateTo(DateTime.Now);
        }

        public void MigrateTo(DateTime versionToMigrateTo)
        {
            ObjectFactory.GetInstance<IMigrationExecutor>().ExecuteMigrations(versionToMigrateTo);
        }
    }
}
